package shuiyuqing.activities;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xkh.xkh.R;

import java.math.BigDecimal;

import com.xkh.xkh.MyApplication;
import managers.UIHandler;
import shuiyuqing.fragments.WatchPointFragment;
import shuiyuqing.fragments.WaterFragment;
import shuiyuqing.fragments.WeatherFragment;
import shuiyuqing.model.BasicInfoModel;
import shuiyuqing.model.Type;
import shuiyuqing.network.beans.IrrBAWaterGate;
import shuiyuqing.network.beans.IrrBRStadia;
import shuiyuqing.network.http.GetRainWeather;
import shuiyuqing.network.http.GetRealWaterGateDam;

/**
 * 水情信息
 * Created by 凯 on 2015/11/7.
 */
public class WaterActivity extends UIHandler.BaseActivity
        implements
//        WaterGateDialogFragment.Delegate,
        WatchPointFragment.Delegate,
        WaterFragment.Delegate,
        WeatherFragment.Delegate{

    public static int FLAG_WATER = 1;
    public static int FLAG_WEATHER = 2;
    // 选中某个fragment
    int flag_fragment = FLAG_WATER;

    public int getFlag_fragment() {
        return flag_fragment;
    }

    public void setFlag_fragment(int flag_fragment) {
        this.flag_fragment = flag_fragment;
    }

    WaterFragment waterFragment;
    WeatherFragment weatherFragment;

    // 返回按钮
    Button btn_back;

    // 刷新界面
    View v_refresh;

    View v_water;
    View v_weather;

    // 打开历史界面
    Button btn_history;


    // 底部字
    TextView tv_bottom_water;
    TextView tv_bottom_weather;

    TextView icon_qixiang;
    TextView icon_shuiwei;

    // 手势
    GestureDetector mGestureDetector;

    public GestureDetector getmGestureDetector() {
        return mGestureDetector;
    }

    public View getV_refresh() {
        return v_refresh;
    }

    // 当前选中监测点
    IrrBRStadia waterstadia;

    IrrBRStadia rainStadia;

    // 当前选中的监测点对应的闸门
    IrrBAWaterGate irrBAWaterGate;

    public IrrBAWaterGate getIrrBAWaterGate() {
        return irrBAWaterGate;
    }

    public IrrBRStadia getWaterstadia() {
        return waterstadia;
    }

    // 设置水情监测点
    public void setWaterstadia(IrrBRStadia waterstadia) {
        this.waterstadia = waterstadia;
        waterFragment.getTv_pointName().setText(waterstadia.getStadiaName());
        // 获取监测点对应的闸门
        if (waterstadia != null) {
            if (waterstadia.getLocCode() == null) return;
            irrBAWaterGate = BasicInfoModel.getwaterGateByStadia(waterstadia.getLocCode());
            if (irrBAWaterGate != null) {
                BigDecimal gateNum = irrBAWaterGate.getWatergateNUM();
                if (gateNum != null) {
                    int num = gateNum.intValue();
                    waterFragment.resetByGateNum(num);
                    waterFragment.getTv_desingWaterLevel().setText(irrBAWaterGate.getDesignWaterLevel()+"");
//                    waterFragment.getTv_gangcheng().setText(waterstadia.getElevation()+"");
                }
            }else
            {
                waterFragment.getTv_desingWaterLevel().setText("--");
            }

        }
        UIHandler.uiHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                showDialog();
                new GetRealWaterGateDam(WaterActivity.this,WaterActivity.this.waterstadia.getStadiaCode()).get();
            }
        }, 0);
    }

    public IrrBRStadia getRainStadia() {
        return rainStadia;
    }

    public void setRainStadia(IrrBRStadia rainStadia) {
        this.rainStadia = rainStadia;
        new GetRainWeather(this,rainStadia.getStadiaCode()).get();
    }

    public WaterFragment getWaterFragment() {
        return waterFragment;
    }

    public WeatherFragment getWeatherFragment() {
        return weatherFragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_water);

        if (savedInstanceState == null) {
            initFragment();
        }
        initview();
        initviewdata();
    }

    // 初始化界面数据,默认设置闸门监测点和气象监测点
    private void initviewdata() {
        // 设置水情监测点
//        IrrBRStadia irrBRStadia = BasicInfoModel.getIrrBRStadiaByCode("10510002");
//        if (irrBRStadia != null)
//        {
//            setWaterstadia(irrBRStadia);
//        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == v_water) {
                setFlag_fragment(FLAG_WATER);
                show(waterFragment);
                showText(tv_bottom_water);

            } else if (v == v_weather) {
                setFlag_fragment(FLAG_WEATHER);
                show(weatherFragment);
                showText(tv_bottom_weather);
            } else if (v == btn_history) {
                Intent intent = new Intent(WaterActivity.this, WaterHistoryActivity.class);
                intent.putExtra("stadia", waterstadia);
                startActivity(intent);
            } else if (v == btn_back) {
                finish();
            }

        }
    };

    // 初始化界面
    private void initview() {
        // 返回按钮
        btn_back = (Button) findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(itemClick);
        btn_history = (Button) findViewById(R.id.id_btn_history);
        btn_history.setOnClickListener(itemClick);
        v_water = findViewById(R.id.id_v_water);
        v_water.setOnClickListener(itemClick);
        v_weather = findViewById(R.id.id_v_weather);
        v_weather.setOnClickListener(itemClick);
        v_refresh = findViewById(R.id.id_v_refresh);
        tv_bottom_water = (TextView) findViewById(R.id.id_tv_bottom_water);
        tv_bottom_weather = (TextView) findViewById(R.id.id_tv_bottom_weather);
        icon_qixiang = (TextView) findViewById(R.id.icon_qixiang);
        icon_shuiwei = (TextView) findViewById(R.id.icon_shuiwei);
        icon_shuiwei.setTypeface(MyApplication.iconfont);
        icon_qixiang.setTypeface(MyApplication.iconfont);
        showText(tv_bottom_water);
    }

    // 刷新界面
    public void onRefresh() {
        if (flag_fragment == FLAG_WATER) {
            if (waterstadia != null) {
                new GetRealWaterGateDam(waterstadia.getStadiaCode()).get();
            } else {
            }
        } else if (flag_fragment == FLAG_WEATHER) {
            if (rainStadia != null) {
                new GetRainWeather(rainStadia.getStadiaCode()).get();
            } else {
            }
        }


    }

    //初始化水情、气象界面
    private void initFragment() {
        waterFragment = new WaterFragment();
        weatherFragment = new WeatherFragment();

        FragmentManager fm = getFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();

        tx.add(R.id.id_content, waterFragment, WaterFragment.class.getSimpleName());
        tx.add(R.id.id_content, weatherFragment, WeatherFragment.class.getSimpleName());
        tx.hide(weatherFragment);

        tx.commit();
    }

    // 隐藏
    public void show(Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();

        tx.hide(waterFragment);
        tx.hide(weatherFragment);
        tx.show(fragment);

        tx.commit();
    }

    // 显示textview
    public void showText(TextView textView) {
        tv_bottom_water.setTextColor(Color.parseColor("#808080"));
        tv_bottom_weather.setTextColor(Color.parseColor("#808080"));
        textView.setTextColor(Color.rgb(0x00, 0x00, 0xff));

    }


    // 选中监测点
    @Override
    public void selPoint(IrrBRStadia stadia) {
        int type = Integer.parseInt(stadia.getStadiaType().trim());
        if (type != Type.RAIN) {
            setWaterstadia(stadia);
        } else {
            getWeatherFragment().getTv_weatherName().setText(stadia.getStadiaName());
            setRainStadia(stadia);
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }


    @Override
    public void setStadia(IrrBRStadia irrBRStadia) {
        setWaterstadia(irrBRStadia);
    }

    @Override
    public void setweatherStadia(IrrBRStadia irrBRStadia) {
        setRainStadia(irrBRStadia);
    }


}
